package com.company.cloud.base.modules.mgr.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.base.modules.base.entity.BizCodeInfo;
import com.company.cloud.base.modules.base.service.BizCodeInfoService;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeMgrQueryParam;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeSaveParam;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeUpdateParam;
import com.company.cloud.base.modules.mgr.controller.resp.BizCodeMgrModel;
import com.company.cloud.base.modules.mgr.service.BizTypeCodeMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 机构信息表  Controller
 * </p>
 *
 * @author zjh
 * @since 2020-05-13
 */
@Authc
@RestController
@RequestMapping("/mgr/base/bizCode")
@Api(tags = "Mgr_Base_业务编码信息")
public class BizCodeMgrController {

    @Autowired
    private BizCodeInfoService bizCodeInfoService;

    @Autowired
    private BizTypeCodeMgrService bizTypeCodeMgrService;

    @PostMapping("/query")
    @ApiOperation(value = "列表查询", response = BizCodeMgrModel.class)
    public BaseResult query(@RequestBody BizCodeMgrQueryParam params) {
        ValidatorUtils.validate(params);
        String[] codes = params.getTypeCodes().split(StrUtil.COMMA);
        List<String> typeCodes = Arrays.asList(codes);
        IPage<BizCodeInfo> data = bizCodeInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<BizCodeInfo>()
                        .in("type_code", typeCodes)
                        .eq(StringUtils.hasText(params.getVname()), "vname", params.getVname())
                        .orderByAsc("weight")
        );
        IPage<BizCodeMgrModel> page = data.convert(companyInfo -> {
            BizCodeMgrModel model = new BizCodeMgrModel();
            BeanUtil.copyProperties(companyInfo, model);
            return model;
        });
        return BaseResult.ok(page);
    }

    @GetMapping("/get")
    @ApiOperation(value = "详情", response = BizCodeMgrModel.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        BizCodeInfo result = bizCodeInfoService.getById(id);
        if (result == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BizCodeMgrModel model = new BizCodeMgrModel();
        BeanUtil.copyProperties(result, model);
        return BaseResult.ok(model);
    }

    @PostMapping("/save")
    @StaffLogc(title = "Mgr_Base_业务编码信息新增", action = "Mgr_Base_业务编码信息新增", actionAdo = "Mgr_Base_业务编码信息列表")
    @ApiOperation(value = "新增", response = BaseResult.class)
    public BaseResult save(@RequestBody BizCodeSaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        bizTypeCodeMgrService.save(params, userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/update")
    @StaffLogc(title = "Mgr_Base_业务编码信息修改", action = "Mgr_Base_业务编码信息修改", actionAdo = "Mgr_Base_业务编码信息列表")
    @ApiOperation(value = "修改", response = BaseResult.class)
    public BaseResult update(@RequestBody BizCodeUpdateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        bizTypeCodeMgrService.update(params, userInfo);
        return BaseResult.ok();
    }

}
